<template>
  <base-layout>
    <div class="problem-container">
      <!-- 左侧题目区域 -->
      <div class="problem-description">
        <problem-detail :problem="problem" />
      </div>

      <!-- 右侧代码编辑区域 -->
      <div class="code-editor">
        <code-editor :problem="problem" :validLanguages="validLanguages" />
      </div>
    </div>
  </base-layout>
</template>

<script>
import { ref, watch } from 'vue';
import { useRoute } from 'vue-router';
import BaseLayout from '@/components/layout/BaseLayout.vue';
import ProblemDetail from '@/components/oj-problem/ProblemDetail.vue';
import CodeEditor from '@/components/oj-problem/CodeEditor.vue';
import { getProblemDetail } from "@/api/hoj-problems.js";
import { XMLParser } from 'fast-xml-parser';

const parser = new XMLParser();

export default {
  name: 'OJProblem',
  components: {
    BaseLayout,
    ProblemDetail,
    CodeEditor
  },
  setup() {
    const route = useRoute();
    const problemId = ref(route.params.id);
    const problem = ref({});
    const validLanguages = ref([]);

    // 监听problemId变化，获取题目详情
    watch(problemId, async (newValue) => {
      if (newValue) {
        const resp = await getProblemDetail(newValue);
        problem.value = {
          ...resp.data.problem,
          exampleData: [].concat(parser.parse(resp.data.problem.examples))
        };
        // 设置可用语言
        validLanguages.value = ['Java', 'cpp', 'python'].map(lang => ({
          value: lang,
          label: lang
        }));
      }
    }, { immediate: true });

    return {
      problemId,
      problem,
      validLanguages
    };
  }
};
</script>

<style scoped>
.problem-container {
  display: flex;
  gap: 24px;
  padding: 24px;
  background: #f0f2f5;
  min-height: calc(100vh - 64px);
}

.problem-description {
  flex: 1;
  min-width: 0;
  max-width: 50%;
  display: flex;
  flex-direction: column;
}

.code-editor {
  flex: 1;
  min-width: 0;
  max-width: 50%;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

@media screen and (max-width: 1200px) {
  .problem-container {
    flex-direction: column;
  }

  .problem-description,
  .code-editor {
    max-width: none;
  }
}
</style> 